/* Includes 头文件------------------------------------------------------------------*/
#include "char.h"

/************************************************
STM32标准程序模板：字符串打印数组值
模块运用到的资源： 数组值
深圳市伊瓦创新科技有限公司
作    者 ： Carson
*************************************************/

//定义全局变量
uint8_t TimCnt_Func=0;          //0：无、1：时间、  2：计数 
uint8_t CntPic_Coexist=0;       //1:图片和计数共存  0：图片和计数不共存
uint8_t CountReflash=0;

struct CountVal CntV={0,0,0,0,0};
struct TimeVal TimV={0,0,0,0,0,0,0,0,0,0};
void TimeHandle(void)
{
  uint8_t x,DateNum[10],TimNum[8],FormatDat[10];
  uint8_t Format[9]={10,10,8,4,2,2,2,2,2};
    DateNum[0]=(TimV.Year/1000);
    DateNum[1]=(TimV.Year/100)%10;
    DateNum[2]=(TimV.Year/10)%100%10;
    DateNum[3]=(TimV.Year%10);
    DateNum[4]= 11;
    DateNum[5]= TimV.Month/10;
    DateNum[6]= TimV.Month%10;
    DateNum[7]= 11;
    DateNum[8]= TimV.Day/10;
    DateNum[9]= TimV.Day%10;
    TimNum[0]= TimV.Hours/10;
    TimNum[1]= TimV.Hours%10;
    TimNum[2]= 10;
    TimNum[3]= TimV.Min/10;
    TimNum[4]= TimV.Min%10;
    TimNum[5]= 10;
    TimNum[6]= TimV.Sec/10;
    TimNum[7]= TimV.Sec%10;
    switch(TimV.Format)  //9种格式
    {
        case 0:for(x=0;x<Format[0];x++)FormatDat[x]=DateNum[x];break;//yyyy/mm/dd
        case 1:for(x=0;x<Format[1];x++)FormatDat[x]=DateNum[x];FormatDat[4]=12;FormatDat[7]=12;break;// yyyy-mm-dd
        case 2:for(x=0;x<Format[2];x++)FormatDat[x]=TimNum[x];break;// hh:mm:ss
        case 3:for(x=0;x<Format[3];x++)FormatDat[x]=DateNum[x];break;// yyyy
        case 4:for(x=0;x<Format[4];x++)FormatDat[x]=DateNum[x+5];break;// mm
        case 5:for(x=0;x<Format[5];x++)FormatDat[x]=DateNum[x+8];break;// dd
        case 6:for(x=0;x<Format[6];x++)FormatDat[x]=TimNum[x];break;// hh
        case 7:for(x=0;x<Format[7];x++)FormatDat[x]=TimNum[x+3];break;// mm
        case 8:for(x=0;x<Format[8];x++)FormatDat[x]=TimNum[x+6];break;// ss
        default:break;
    }
    Font_ScalingHandle(TimV.CharHigh,FormatDat,Format[TimV.Format],TimV.X_Coord,TimV.Y_Coord);
}

uint16_t powHandle(uchar num1,uchar num){
	uint16_t value=1;
	if(num){
		for(uchar x=0;x<num;x++)
			value = num1*value;
	}
	return value;
}

uint8_t Preflag=0;
uint32_t Last_PrintDateNum=0;
void CountHandle(void)
{
  uint8_t CntNum[4],flag=0;
    for(uint8_t x=0;x<4;x++)
    {
        CntNum[x]=CntV.Initialval/(uint16_t)(powHandle(10,3-x))%10;
        if(!flag)
        {//判断需要打印的字符个数
            CntNum[x]>0?(flag=4-x):0;
        }
    }
    if(flag==0)flag=1;//这种情况就是0，所以一定要显示0这个数字
    //计算即将打印的数据量
    if(Preflag!=flag){//避免每一次位数相同的情况下还要去进行一遍清除动作，浪费资源。
        uint32_t num = CntV.X_Coord+((CntV.CharHigh<=10)?(OX/10*CntV.CharHigh*flag):((OX/8*(CntV.CharHigh-10)+OX)*flag))+62/*传图左侧补白边*/;//根据字符数个数计算
        //***************图案和计数器并存***************//
		if(Debug_set)printf("num:%d / Coexist:%d / Preflag:%d / flag:%d\n",num,CntPic_Coexist,Preflag,flag);
		if(CntPic_Coexist){
            /****位数减少的情况******/
            if(Preflag>flag){
                //***************计数器位数减少后在内***************//
                if((num*39) < Last_PrintDateNum){
                    data_length = Last_PrintDateNum;
                    //---------------进行刷新原图数据------------------------//
                    if(Debug_set)printf("FLASH_ReadByte!!\n");
                    FLASH_ReadByte();//flash读取图片数据
                }
                //***************计数器位数减少后在外***************//
                else{
                    data_length -= (((CntV.CharHigh<=10)?(OX/10*CntV.CharHigh):(OX/8*(CntV.CharHigh-10)+OX))*39);//总数据大小减少一个位数的大小位置
					if(Debug_set)printf("data_length:%d\n",data_length);
				}
            }
            /****位数增加的情况******/
            else{
                //***************计数器位数增加后在内***************//
                if((num*39) < Last_PrintDateNum){
                    //无需操作
                    if(Debug_set)printf("Last_PrintDateNum:%d\n",Last_PrintDateNum);
                }
                //***************计数器位数增加后在外***************//
                else{
                    for(uint32_t y=0;y<(num*39-data_length);y++)
                        RxBuf[7+data_length+y] = 0x0;
					data_length = num*39;//刷新数据大小
					if(Debug_set)printf("data_length1:%d\n",data_length);
                }
            }
        }else{//只有计数器，计算要打印的数据量
            data_length = num*39;
			if(Debug_set)printf("data_length2:%d\n",data_length);
        }
    }
    Preflag = flag;//保存当前的字符位数

	if(CntV.Initialval==9999||CntV.Initialval==0)
		BeepExp_set(100,2);//最后一个数字响多2声提醒客户
		
	if(Debug_set)printf("Initialval:%d / Dir:%d / XY_Coord:%d,%d / High:%d\n",CntV.Initialval,CntV.CountDir,CntV.X_Coord,CntV.Y_Coord,CntV.CharHigh);
    //处理缩放数据函数
    Font_ScalingHandle(CntV.CharHigh,&CntNum[4-flag],flag,CntV.X_Coord,CntV.Y_Coord);
	
    if(!CntV.CountDir&&CntV.Initialval!=9999)//改变计数器值
       CntV.Initialval++;
    else if(CntV.CountDir&&CntV.Initialval!=0)
       CntV.Initialval--;
}

uint8_t TimCntCmdHandle(char *dat)
{
    TimCnt_Func = 0;    //清除标志位
    if(dat[0]==0xA1||dat[0]==0xA2) //有时间/计数情况
    {
       //存储X、Y轴坐标值
         TimV.X_Coord = dat[1]<<8;
         TimV.X_Coord |= dat[2];
         TimV.Y_Coord = dat[3]<<8;
         TimV.Y_Coord |= dat[4];
         CntV.X_Coord = dat[5]<<8;
         CntV.X_Coord |= dat[6];
         CntV.Y_Coord = dat[7]<<8;
         CntV.Y_Coord |= dat[8];
       //存储字符大小
         TimV.CharHigh = dat[9];
         CntV.CharHigh = dat[10];
      //存储时间、计数参数
         TimV.Format = dat[11];//时间格式
         TimV.Year = dat[12]*100;
         TimV.Year += dat[13];
         TimV.Month = dat[14];
         TimV.Day = dat[15];
         TimV.Hours = dat[16];
         TimV.Min = dat[17];
         TimV.Sec = dat[18]+2;//因为发送过来也要时间，所以自动加2秒
         CntV.Initialval = dat[19]<<8;//计数初始值
         CntV.Initialval |= dat[20];
         CntV.CountDir = dat[21];//计数方向
      //根据对应字符大小来确认是否有选择对应功能
         if(TimV.CharHigh)
              TimCnt_Func |= 1;
         if(CntV.CharHigh)
              TimCnt_Func |= 2;
         
         if(dat[0] == 0xA1){//有图情况,返回真
            CntPic_Coexist = 1;
            return 1;
         }
         else if(dat[0] == 0xA2){//无图情况,返回假
            CntPic_Coexist = 0;
            return 0;
         }
    }
    return 1;//无时间&计数，则默认有图，返回真
}

void FLASH_Counter_Storage(void)
{
    uint8_t counter[20]={0};
    if(Print_FlashSwitch)
    {
      if(StartPrintFlag){
        if(!CntV.CountDir&&CntV.Initialval!=9999)//递增模式
            CntV.Initialval=CntV.Initialval-1;
        else if(CntV.CountDir&&CntV.Initialval!=0)//递减模式
          CntV.Initialval=CntV.Initialval+1;
      }
      counter[0]  = 0xEE;
      counter[1]  = (uint8_t)(CntV.Initialval>>8);     //初始值高
      counter[2]  = (uint8_t)(CntV.Initialval);        //初始值低
      counter[3]  = (uint8_t)(CntV.X_Coord>>8);
      counter[4]  = (uint8_t)(CntV.X_Coord);
      counter[5]  = (uint8_t)(CntV.Y_Coord>>8);
      counter[6]  = (uint8_t)(CntV.Y_Coord);
      counter[7]  = (uint8_t)(CntV.CountDir);
      counter[8]  = (uint8_t)(CntV.CharHigh);
      counter[9]  = CntPic_Coexist;
      counter[10]  = (uint8_t)(Last_PrintDateNum>>24);
      counter[11] = (uint8_t)(Last_PrintDateNum>>16);
      counter[12] = (uint8_t)(Last_PrintDateNum>>8);
      counter[13] = (uint8_t)(Last_PrintDateNum);
      counter[14]  = (uint8_t)(data_length>>24);
      counter[15] = (uint8_t)(data_length>>16);
      counter[16] = (uint8_t)(data_length>>8);
      counter[17] = (uint8_t)(data_length);
      counter[18] = TimCnt_Func;
      counter[19] = only_count;
	  esp_nvs_write_handler(NVS_key_Counter,counter,sizeof(counter));
    }
}
void FLASH_PicCnt_Read(void)
{
  esp_err_t err;
  uint8_t counter[20]={0};
  if(Print_FlashSwitch)
    {
        err = esp_nvs_read_handler(NVS_key_Counter,counter);
        if(err == ESP_OK){
            CntV.Initialval  = (counter[1]<<8|counter[2]);
            CntV.X_Coord     = counter[3]<<8|counter[4];
            CntV.Y_Coord     = counter[5]<<8|counter[6];
            CntV.CountDir    = counter[7];
            CntV.CharHigh    = counter[8];
            CntPic_Coexist   = counter[9];
            Last_PrintDateNum = 0;
            Last_PrintDateNum |= counter[10]<<24;
            Last_PrintDateNum |= counter[11]<<16;
            Last_PrintDateNum |= counter[12]<<8;
            Last_PrintDateNum |= counter[13];
            data_length = 0;
            data_length |= counter[14]<<24;
            data_length |= counter[15]<<16;
            data_length |= counter[16]<<8;
            data_length |= counter[17];
            TimCnt_Func = counter[18];  
            only_count  = counter[19];
            Preflag     =0;
           
            uint32_t num = CntV.X_Coord+((CntV.CharHigh<=10)?(OX/10*CntV.CharHigh*4):((OX/8*(CntV.CharHigh-10)+OX)*4))+62/*传图左侧补白边*/;//最多4个计数器值
            //清空计数器值所占的空间，避免前面有传过的图预留下来
            for(uint32_t y=0;y<num*39;y++)
				RxBuf[7+y] = 0x0;
        }
        if(only_count){
            ReadyPrintFlag = 1;
        }
        else
            FLASH_ReadByte();//flash读取图片数据
        
    }
}


void TimCntTask(void)   //刷新计数/时间戳值
{
//    if(TimCnt_Func&0x01)
//    {
//        TimeHandle();
//    }else Preflag=0;//是避免出现先单独传了计数器，然后又单独传了图案，再传回单独计数器的时候会残留图案，所以这个标志要清除
    if(TimCnt_Func&0x02)
    {
        CountHandle();
    }else Preflag=0;
}

